package com.zlx.mall.basic.resource;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zlx.mall.base.entity.BasicResource;
import com.zlx.mall.base.service.IBasicResourceService;
import com.zlx.mall.basic.datasource.constant.DatabaseConstants;
import com.zlx.mall.basic.datasource.enums.DeleteEnum;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author qingke
 **/
@Service
public class ResourceServiceImpl extends AbstractResourceService {

    @Resource
    private IBasicResourceService resourceService;

    @Nullable
    @Override
    protected String queryFromDatabase(ResourceEnum resource) {
        return Optional.ofNullable(resourceService.getOne(new QueryWrapper<BasicResource>()
                        .eq(BasicResource.RESOURCE_KEY, resource.getCode())
                        .eq(BasicResource.IS_DELETE, DeleteEnum.EXISTED.getLongValue())
                        .orderByDesc(BasicResource.ID)
                        .last(DatabaseConstants.SQL_LIMIT_ONE)))
                .map(BasicResource::getResourceValue)
                .orElse(null);
    }
}
